import { Component, OnInit, Pipe, PipeTransform, ViewChild } from '@angular/core';
import { uniqueId, map } from 'lodash';

interface DemoUser {
  id: string;
  name: string;
  age: number;
  expand: boolean;
  random: number;
}

@Pipe({ name: 'userProp' })
export class UserPropPipe implements PipeTransform {
  transform(value: DemoUser[], exponent: string = 'name') {
    return map(value, item => item[exponent]).join(',');
  }
}

@Component({
  selector: 'app-demo26',
  templateUrl: './demo26.component.html',
  styles: [
    `
      .section-info {
        margin-bottom: 0.16rem;
      }
    `,
  ],
})
export class Demo26Component implements OnInit {
  @ViewChild('lvTable', { static: false }) table;

  data: DemoUser[];
  selection: DemoUser[];

  constructor() {}

  ngOnInit() {
    this.initData();
  }

  initData() {
    this.data = Array.from({ length: 5 }).map((item, key) => {
      return this.initUser(key);
    });
  }

  initUser(key: number) {
    return {
      id: uniqueId('uid'),
      name: `user${key}`,
      age: 10 + key,
      expand: false,
      random: parseInt(Math.random() * 100 + '', 10),
    };
  }

  _radioSelect(item) {
    this.table.toggleSelection(item);
  }
}
